﻿#region Copyright Notice
// 
// 
// Copyright (c) 2009-2010 Anindya Chatterjee
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// 
// 
// 
#endregion
using System.Net;

namespace Event.Plugin.Configuration
{
    ///<summary>
    /// Class to hold the required proxy settings data
    ///</summary>
    public class WebProxySettings
    {
        /// <summary>
        /// Gets or sets the host ip.
        /// </summary>
        /// <value>The host ip.</value>
        public string Host { get; set; }

        /// <summary>
        /// Gets or sets the port.
        /// </summary>
        /// <value>The port.</value>
        public int Port { get; set; }

        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        /// <value>The id.</value>
        public string Id { get; set; }

        /// <summary>
        /// Gets or sets the password.
        /// </summary>
        /// <value>The password.</value>
        public string Password { get; set; }

        private WebProxy Proxy
        {
            get
            {
                return new WebProxy(Host, Port)
                           {
                               Credentials = new NetworkCredential
                                                 {
                                                     UserName = Id,
                                                     Password = Password
                                                 }
                           };
            }
        }

        private static WebProxySettings GetProxySettings(WebProxy proxy)
        {
            return new WebProxySettings
                       {
                           Host = proxy.Address.Host,
                           Port = proxy.Address.Port,
                           Id = ((NetworkCredential) proxy.Credentials).UserName,
                           Password = ((NetworkCredential) proxy.Credentials).Password
                       };
        }

        /// <summary>
        /// Performs an implicit conversion from <see cref="System.Net.WebProxy"/> to <see cref="Event.Plugin.Configuration.WebProxySettings"/>.
        /// </summary>
        /// <param name="p">The <see cref="System.Net.WebProxy"/> object</param>
        /// <returns>The result of the conversion.</returns>
        public static implicit operator WebProxySettings(WebProxy p)
        {
            return p == null ? null : GetProxySettings(p);
        }

        /// <summary>
        /// Performs an implicit conversion from <see cref="Event.Plugin.Configuration.WebProxySettings"/> to <see cref="System.Net.WebProxy"/>.
        /// </summary>
        /// <param name="p">The <see cref="Event.Plugin.Configuration.WebProxySettings"/> object</param>
        /// <returns>The result of the conversion.</returns>
        public static implicit operator WebProxy(WebProxySettings p)
        {
            return p == null ? null : p.Proxy;
        }
    }
}